package com.qbkj.base_lib.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by St on 2018/4/19.
 */

public class CommonUtils {
    //条形码返回值
    public static final int BAR_CODE = 2002;
    //二维码返回值
    public static final int QR_CODE = 2003;

    //获取 屏幕宽度
    public static int getScreenWidth(Context context) {
        WindowManager manager = ((Activity) context).getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    //获取屏幕高度
    public static int getScreenHeight(Context context) {
        WindowManager manager = ((Activity) context).getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    private static float[] pixi;

    //获取屏幕宽高缩放比例(AutoLayout框架 使用）
    public static float[] getScreenPixi(Context context) {
        if (pixi == null || pixi.length != 2) {
            pixi = new float[]{1, 1};
            try {
                ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
                pixi[0] = (getScreenWidth(context) / (float) appInfo.metaData.getInt("design_width"));
                pixi[1] = (getScreenHeight(context) / (float) appInfo.metaData.getInt("design_height"));
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return pixi;
    }

    /**
     * 检查权限
     *
     * @param context
     * @param check_permissions
     * @param sdk_int           该版本以上检查权限
     * @param requestCode       设置完毕返回码
     * @return true 表示已有 所查权限
     */
    public static boolean hasPermission(Context context, List<String> check_permissions, int sdk_int, int requestCode) {
        if (Build.VERSION.SDK_INT >= sdk_int) {
            List<String> permissions = new ArrayList<>();
            for (int i = 0; i < check_permissions.size(); i++) {
                String permission = check_permissions.get(i);
                if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(context, permission)) {
                    permissions.add(permission);
                }
            }
            String[] s = permissions.toArray(new String[0]);
            if (permissions.size() != 0) {
                ActivityCompat.requestPermissions((Activity) context, permissions.toArray(new String[0]), requestCode);
                return false;
            }
        }
        return true;
    }

    public static int dp2px(float value, Context context) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }

}
